// 设置ajax CSRFToken
var csrftoken = getCookie("csrftoken") || '';
// var upload_domain = 'http://ozhwe6jzx.bkt.clouddn.com';
var upload_domain = 'http://qiniu.liudewang.top';
$.ajaxSetup({
	headers: {
		"X-CSRFToken": csrftoken
	}
})
$(function(){
	if(!String.prototype.trim) {
		String.prototype.trim = function () {
			return this.replace(/^\s+|\s+$/g,'');
		};
	}
	$('form').on('submit', function(e){
		e.preventDefault();
	})
	$(".center-btn-group").mouseover(function(e){
		$(this).addClass("open");
	})
	$(".center-btn-group").mouseout(function(e){
		$(this).removeClass("open");
	})
	// 限制输入数字
	$(".onlynum-input").on("keydown", function(e){
		var keyCode = e.keyCode;
       	if ( !isNumber(keyCode) ) return false 
	})
	// 退出登录
	$("#logout").on("click", function(e){
		e.stopPropagation();
		userLogout({
			success: function(res){
				window.location.href = "signin.html"
			},
			error: function(res){
				alert("退出登录失败，请重试");
			}
		})
	})
	// 返回上一页
	$(".return-div").on("click",function(e){
		e.stopPropagation();
		window.history.go(-1);
	})
})
//检测电话号码输入是否正确
function checkPhone(phone){
    var phone = phone || '';
	//0 1 2都不行
    return /^1[2|3|4|5|6|7|8|9]\d{9}$/.test(phone)
}
//输入是否数字 有可能会卡
function isNumber(code) {
    // 数字
    if (code >= 48 && code <= 57 ) {return true};
    // 小数字键盘
    if (code >= 96 && code <= 105) {return true};
    // Tab, Backspace, del, 左右方向键, enter
    if (code == 8 || code == 9 || code == 13 || code == 46  || code == 37 || code == 39) {return true};
    return false
}
// 退出登录
function userLogout(option){
	$.getJSON('/api/auth/logout/', function(res){
		option.success(res);
	}).error(function(res){
		option.fail(res);
	})
}
// 获取验证吗
function getVerifyCode(option){
	$.post('/api/auth/v_code/', {
		phone: option.phone,
		operation: option.operation
	}, function(res){
		option.success(res);
	}).error(function(res){
		option.fail(res);
	})
}
// 获取用户信息
function getUserInfo(option){
	$.getJSON("/api/auth/user_info/", function(res){
		option.success(res);
	}).error(function(res){
		var res = JSON.parse(res.responseText)
		if(res.code == 401){
			window.location.href = "signin.html";
		}else{
			option.fail(res);			
		}
	})
}
// 获取试题列表
function getExamList(option){
	var prama = {
		page: option.page,
		size: option.size
	}
	if(typeof option.status !== undefined){
		prama.status = option.status
	}
	if(typeof option.start_date !== undefined){
		prama.start_date = option.start_date
	}
	if(typeof option.end_date !== undefined){
		prama.end_date = option.end_date
	}
	if(typeof option.name !== undefined){
		prama.name = option.name
	}
	$.ajax({
		url: "/api/line_up/exams/",
		type: "GET",
		dataType: "JSON",
		data: prama,
		success: function(res){
			option.success(res)
		},
		error: function(res){
			option.fail(res)
		}
	})
}
// 获取某个考试信息
function getExamInfo(option){
	$.ajax({
		url: "/api/line_up/exams/"+option.code+"/",
		type: "GET",
		dataType: "JSON",
		success: function(res){
			option.success(res)
		},
		error: function(res){
			option.fail(res)
		}
	})
}
// 获取学生列表
function getStudentList(option){
	var prama = {
		page: option.page,
		size: option.size,
		exam: option.code
	}
	if(typeof option.status !== undefined){
		prama.status = option.status
	}
	if(typeof option.name !== undefined){
		prama.name = option.name
	}
	if(typeof option.exam_number !== undefined){
		prama.exam_number = option.exam_number
	}
	$.ajax({
		url: "/api/line_up/candidates/",
		type: "GET",
		dataType: "JSON",
		data: prama,
		success: function(res){
			option.success(res)
		},
		error: function(res){
			option.fail(res)
		}
	})
}
// 获取叫号名单
function getCalledStudent(option){
	var url = "/api/line_up/call/"+option.code+"/"
	$.ajax({
		url: url,
		type: "GET",
		dataType: "JSON",
		data: {
			page: 1,
			size: 10
		},
		success: function(res){
			option.success(res)
		},
		error: function(res){
			var res = JSON.parse(res.responseText)
            alert(res.msg)
		}
	})
}
// 取消叫号名单
function cancleCalledStudent(option){
	var url = "/api/line_up/call/"+option.code+"/"
	$.ajax({
		url: url,
		type: "DELETE",
		dataType: "JSON",
		data: {
			page: 1,
			size: 10
		},
		success: function(res){
			option.success(res);
		},
		error: function(res){
			var res = JSON.parse(res.responseText)
            alert(res.msg)
		}
	})
}
// 设置分页的pagination
function setPagination(option){
	var previous = option.previous;
	var next = option.next;
	var count = option.count;
	var nowPage = option.nowPage;
	var page_count = option.page_count;
	if(count === 0 || count === undefined){
		$(".hasnot-table").removeClass("hide");
		$(".has-table").addClass("hide");
		return
	}
	if(previous){
		$("#previous_page").attr("data-url", previous);
		$("#previous_page").removeClass("disabled");
	}else{
		$("#previous_page").addClass("disabled");
	}
	if(next){
		$("#next_page").attr("data-url", next);
		$("#next_page").removeClass("disabled");
	}else{
		$("#next_page").addClass("disabled");
	}
	if(count/10<10){
		page_count = Math.ceil(count/10);
	}
	$(".page-li").remove();
	for(var i = 0; i < page_count; i++){
		var li = $("<li class='page-li'>");
		var a = $("<a href='javascript:void(0);'></a>");
		var a_text = i+1;
		if(nowPage > 6){
			a_text = i+nowPage-5;
		}
		a.text(a_text);
		li.append(a);
		if(nowPage == a_text){
			li.addClass("active");
		}
		$("#next_page").before(li);
	}
	option.continueFun()
}

// 获取考试状态
function getExamStatus(status){
	return status === 0 ? "未开始" : ( status === 2 ? "已结束" : "进行中")
}
// 获取考生状态
function getStuStatus(status){
	return status === 0 ? "未签到" : ( status === 2 ? "已考试" : "已签到")
}

// 获取cookie
function getCookie(name){
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg)){
		return unescape(arr[2]);
	}else{
		return null;		
	}
}
// 比较时间先后
function compareTime(begin, end){
	var _begin = +new Date(begin.replace(/-/g, '/'));
	var _end = +new Date(end.replace(/-/g, '/'));
	var times =  _end - _begin;
	if(times < 1000*60*5){
		return false
	}
	return true
}

/*获取url参数 str带引号*/
function GetQueryString(str) {
    var href = String(window.document.location.href);
    var rs = new RegExp("([\?&])(" + str + ")=([^&]*)(&|$)", "gi").exec(href);
    if (rs) {
        return decodeURI(rs[3]);
    } else {
        return '';
    }
}

